﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Models;
namespace DAL
{
    public class DALCMS_User
    {
        KQCMS db = new KQCMS();
        public List<CMS_User> GetCMS_Users()
        {
            using (var db = new KQCMS())
            {
                return db.CMS_User.Where(x => x.Uid > 2).ToList();

            }
        }
        public static List<CMS_User> GetCMS_UserAll()
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_User.ToList();

                return list;
            }
        }
        public static List<CMS_User> GetCMS_UserAllWhere(string Uname)
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_User.AsQueryable();
                if (!string.IsNullOrEmpty(Uname))
                {
                    list = list.Where(x => x.Uname.Contains(Uname));
                }
                return list.ToList();
            }
        }
        public static CMS_User login(string name, string pwd)
        {
            using (var db = new KQCMS())
            {
                db.Database.CreateIfNotExists();
                //db.CMS_User.Where(x => x.Uname == name && x.upwd == pwd) ;
                return db.CMS_User.FirstOrDefault(x => x.Uname == name && x.upwd == pwd);

            }
        }
        public static int Login(string Uname, string upwd)
        {
            using (var db = new KQCMS())
            {
                int stu = db.CMS_User.Where(s => s.Uname == Uname && s.upwd == upwd).Count();
                return stu;
            }

        }
        public static int Insert(CMS_User c)
        {
            using (var db = new KQCMS())
            {
                var stu = new CMS_User() { Uname = c.Uname, upwd = c.upwd, nname = c.nname, mobile = c.mobile, face = c.face, Admin = c.Admin };
                db.CMS_User.Add(stu);
                return db.SaveChanges();
            }

        }
        public static List<CMS_User> Selectid(int id)
        {
            using (var db = new KQCMS())
            {
                var a = db.CMS_User.Where(x => x.Uid == id).ToList();
                return a;
            }

        }
        public static int update(int id, string uname, string upwd, string nname, string mobile, string face, bool admin)
        {
            using (var db = new KQCMS())
            {
                var s = db.CMS_User.Where(x => x.Uid == id).FirstOrDefault();
                s.Uid = id;
                s.Uname = uname;
                s.upwd = upwd;
                s.nname = nname;
                s.mobile = mobile;
                s.face = face;

                s.Admin = admin;
                return db.SaveChanges();
            }

        }
        public static int Datete(int id)
        {
            using (var db = new KQCMS())
            {
                var stu = new CMS_User() { Uid = id };
                db.CMS_User.Attach(stu);
                db.CMS_User.Remove(stu);
                return db.SaveChanges();
            }

        }
        public static string GetPageCMS_Articles(int pageindex, int pagesize, string Uanem)
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_User.AsQueryable();
                //所有数据
                //list = list.Where(x => x.state == 1 || x.state == 2);
                if (!string.IsNullOrEmpty(Uanem))
                {
                    string name = Uanem;
                    list = list.Where(x => x.Uname.Contains(name));
                }
                //某一页数据
                var data = list.OrderBy(x => x.Uid)
               .Skip((pageindex - 1) * pagesize)//排除前面多少行
               .Take(pagesize)//只取前面多行
               .ToList();             //得到数据的条数

                //总行数
                int rowCount = list.ToList().Count();
                //总页数
                var pageSum = 0;
                //通过计算，得到分页应该需要分几页，其中不满一页的数据按一页计算
                if (rowCount % pagesize != 0)
                {
                    pageSum = rowCount / pagesize + 1;
                }
                else
                {
                    pageSum = rowCount / pagesize;
                }

                //转成Json格式    总页数：9页                  当前页码                                           当前页的数据              
                var strResult = "{\"pageSum\":" + pageSum + ",\"CurrentPage\":" + pageindex + ",\"list\":" + JsonConvert.SerializeObject(data) + "}";
                return strResult;
            }
        }
    }
}

